import pandas as pd
import datetime
import numpy as np
# https://www.zhifure.com/snzfj/73515.html
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.svm import LinearSVR, SVR, NuSVR
from sklearn import linear_model
import warnings
warnings.filterwarnings('ignore')

A = pd.read_csv('data/train_rice1.csv', header=0, encoding='gbk')
cols = ['区县id', '2015年早稻', '2015年晚稻', '2016年早稻', '2016年晚稻', '2017年早稻', '2017年晚稻']
A = A.ix[:, cols]
counties = A.ix[:, 0]

# fx_map = {
# 'N':   1,
# 'NNE': 2,
# 'NE' : 4,
# 'NNW': 5,
# 'C':   6,
# 'ESE': 7,
# 'SE':  8,
# 'E':   9,
# 'ENE': 10,
# 'SSE': 11,
# 'NW':  12,
# 'S':   13,
# 'WSW': 14,
# 'SW':  15,
# 'W':   16,
# 'SSW': 17,
# 'WNW': 18,
# '*'  : 0,
# '/'  : 0
# }
#
# B = pd.read_csv('data/train_weather1.csv', header=0, encoding='gbk')
# B.insert(2, 'date', B.apply(lambda x: datetime.date(x[2], x[3], x[4]).strftime("%Y%m%d"), axis=1))
# B.drop(['年份', '月份', '日期'], axis=1, inplace=True)
# B.replace(fx_map, inplace=True)
#
# cols = ['区县id', '站名id', 'date', '日照时数（单位：h)','02时风向', '08时风向', '14时风向',
#        '20时风向', '日平均风速(单位：m/s)', '日降水量（mm）', '日最高温度（单位：℃）', '日最低温度（单位：℃）',
#         '日平均温度（单位：℃）', '日相对湿度（单位：%）', '日平均气压（单位：hPa）']
# # 'county8', 'county1', 'county34'
# B1 = B.ix[B['站名id'] == 1, cols]
# B1.dtypes
# B1.ix[:, [0,1,2]] = B1.ix[:, [0,1,2]].astype(np.object)
# B1.ix[:, 3:] = B1.ix[:, 3:].astype(np.float64)
#
# data = []
label = []
# num1 = 2
#
# month = list(map(lambda x: '0'+str(x)+'01', range(1, 10))) + ['1001', '1101', '1201', '1301']
# for i in range(12):
#     for year in ['2015', '2016', '2017', '2018']:
#         B2015 = B1.ix[(B1['date'] > '{year}{month}'.format(year=year, month=month[i]))
#                       & (B1['date'] < '{year}{month}'.format(year=year, month=month[i+1])), :]
#         B2015grp = B2015.groupby('区县id')
#         B2015cnt = B2015grp.describe()
#         cols_sp = list(range(0, 8*(len(cols)-3), 8))
#         cols_all = list(range(0, 8*(len(cols)-3), 1))
#         cols_diff = [a for a in cols_all if a not in cols_sp]
#         B2015fea0 = B2015cnt.ix[:, cols_diff]
#         B2015fea = B2015fea0.ix[counties, :]
#         data += [B2015fea]
#
# feacols = []
# tj = ['mean', 'std', 'min', '25%', '50%', '75%', 'max']
# for year in ['2015', '2016', '2017', '2018']:
#     for mon in month[:-1]:
#         for a in cols[3:]:
#             for fea in tj:
#                 feacols += [year+'_'+mon+'_'+a+'_'+fea]
#
# all_fea = pd.concat(data, axis=1)
# all_fea.columns = feacols
# all_fea.to_csv('data/all_fea.csv', header=True, index=False)

all_fea = pd.read_csv('data/12all_fea.csv', header=0)

A.set_index('区县id', inplace=True)
men_zao = A.ix[:, [1-1, 3-1, 5-1]].mean(axis=1)
men_zao.to_csv('data/men_zao.csv', header=False, index=True)

men_wan = A.ix[:, [2-1, 4-1, 6-1]].mean(axis=1)
men_wan.to_csv('data/men_wan.csv', header=False, index=True)


for i in range(A.shape[0]):
    A.ix[i, [1-1, 3-1, 5-1]] = A.ix[i, [1-1, 3-1, 5-1]] -men_zao[i]
    A.ix[i, [2-1, 4-1, 6-1]] = A.ix[i, [2-1, 4-1, 6-1]] -men_wan[i]

T = [A.ix[:, 0], A.ix[:, 2], A.ix[:, 4], A.ix[:, 1], A.ix[:, 3], A.ix[:, 5]]
all_label = pd.concat(T, axis=0, ignore_index=True)
all_label.to_csv('data/men_all_label.csv', header=True, index=False)


# 7个统计维度*12大维度*12个月*4年
newheader = list(range(7*12*(3)))
data = []
j = -1
for m in [['0401', '0501', '0601'], ['0701', '0801', '0901']]:
    j += 1
    for year in ['2015', '2016', '2017', '2018']:
        data += [all_fea.filter(regex='|'.join(['^'+year+'_'+a+'.*' for a in m]))]
        data[-1].columns = newheader

cols = all_fea.filter(regex='|'.join(['^'+year+'_'+a+'.*' for a in m])).columns
pd.DataFrame(cols).to_csv('data/men_all_fea_header.csv')

all_fea_month = pd.concat([data[0], data[1], data[2], data[4], data[5], data[6]], axis=0, ignore_index=True)
all_fea_month.to_csv('data/men_all_fea_month.csv', header=True, index=False)

all_fea_month_tst = data[3]
all_fea_month_tst.to_csv('data/men_all_fea_month_tst.csv', header=True, index=False)

all_fea_month_tst = data[7]
all_fea_month_tst.to_csv('data/men_all_fea_month_tst2.csv', header=True, index=False)